#include "net/TcpClient.h"
#include "net/EventLoop.h"
#include "Base/Logger.h"


using namespace toym;
using namespace toym::net;

void connectionCallback(const TcpConnectionPtr& conn) {
    LOG_INFO << "in function " << __func__ ;
    if (conn->connected()) {
        LOG_INFO << "connection to " << conn->getPeerAddr().toIpPort();
        conn->send("hello");
    } else {
        LOG_INFO << "disconnect to " << conn->getPeerAddr().toIpPort();
    }
}

void messageCallback(const TcpConnectionPtr& conn, Buffer* buf, Timestamp ts) {
    LOG_INFO << "in function " << __func__ 
             << ", recv: " << buf->readAsString()
             << ", from " << conn->getPeerAddr().toIpPort();
    conn->shutdown();
}

int main() {
    EventLoop loop;
    InetAddress serverAddr("127.0.0.1", 10077);
    TcpClient client(&loop, serverAddr, "MyClient");
    client.setConnectionCallback(connectionCallback);
    client.setMessageCallback(messageCallback);

    LogPuter::setLevel(LogPuter::LogLevel::INFO);

    client.start();
    loop.loop();
}